package com.gzw.scm.wuyong;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import com.gzw.scm.R;
import com.gzw.scm.adapter.QQJGAdapter;
import com.gzw.scm.base.BaseActivity;
import com.gzw.scm.bean.Constant;
import com.gzw.scm.bean.JgInfo;
import com.gzw.scm.utils.HttpUtils;
import com.gzw.scm.utils.JsonUtil;
import com.gzw.scm.utils.T;
import com.gzw.scm.utils.UrlUtils;
import com.gzw.scm.view.SwipeRefreshLayout;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/5/10.
 */

public class QQJGActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener{
    private Context mContext;
    private RecyclerView mRecyclerView;
    private Toolbar toolbar;
    private SwipeRefreshLayout mSwipeLayout;
    private QQJGAdapter qqjgAdapter;
    private Intent intent;
    private String type;


    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_qqjg);
    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_jg);
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("选择申请部门");
        setSupportActionBar(toolbar);

        mRecyclerView.setHasFixedSize(true);
        //设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加默认分割线
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mSwipeLayout.setColor(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setMode(SwipeRefreshLayout.Mode.PULL_FROM_START);
        mSwipeLayout.setLoadNoFull(false);

        GetTask getTask = new GetTask();
        getTask.execute();

    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onRefresh() {
            GetTask getTask = new GetTask();
            getTask.execute();
    }

    /**
     * 获取下级部门数据请求
     * 分部-内部调拨
     */
    public class GetTask extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
            SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY, Context.MODE_PRIVATE);
            int did = sharedPreferences.getInt(Constant.DID_KEY, 0);
            String url = "allot=true&did=" + did;
            Log.d("Debug", "doInBackground: "+url);
            return HttpUtils.doPost(UrlUtils.getInstance().getNewJavaUrl("branchOperation.do", "PDABranch"), url);
        }

        @Override
        protected void onPostExecute(final String result) {
            //{"result":"false","value":"暂无机构请求调拨箱子"}
            if (result == null || result.length() == 0) {
                T.showShort(mContext, "连接服务器异常");
                return;
            }
            JigInfo jigInfo = JsonUtil.getPerson(result, JigInfo.class);
            if(jigInfo.getResult().equals("false")){
                T.showShort(mContext,jigInfo.getValue());
            }else {
                String jgStr = jigInfo.getValue();
//                String jgStr ="1,部门1,1,2,部门2,2";
                String[ ] str_Array = jgStr.split(",");
                List<String> ls_id = new ArrayList<>();
                List<String> ls_mc = new ArrayList<>();
                List<String> ls_num = new ArrayList<>();

                for(int i=0;i<str_Array.length;i++){
                    if(even(i)==0){
                        ls_id.add(str_Array[i]);
                    }else if(even(i)==1){
                        ls_mc.add(str_Array[i]);
                    }else if(even(i)==2){
                        ls_num.add(str_Array[i]);
                    }
                }

                List<JgInfo> ls_jg = new ArrayList<>();
                for(int i=0;i<ls_id.size();i++){
                    JgInfo jgInfo = new JgInfo();
                    jgInfo.setId(ls_id.get(i));
                    jgInfo.setMc(ls_mc.get(i));
                    jgInfo.setNum(ls_num.get(i));
                    ls_jg.add(jgInfo);
                }
            final List<JgInfo> all_lsjg = new ArrayList<>();
            all_lsjg.addAll(ls_jg);
            if (qqjgAdapter == null) {
                mRecyclerView.setAdapter(qqjgAdapter = new QQJGAdapter(all_lsjg));
            } else {
                qqjgAdapter.notifyDataSetChanged();
            }

            qqjgAdapter.setOnRecyclerViewListener(new QQJGAdapter.OnRecyclerViewListener() {
                @Override
                public void onItemClick(int position) {
                    Intent intent = new Intent(mContext, DBZZXActivity.class);
                    intent.putExtra("eid",all_lsjg.get(position).getId());
                    intent.putExtra("num",all_lsjg.get(position).getNum());
                    startActivityForResult(intent, 1);
                }

                @Override
                public boolean onItemLongClick(int position) {
                    return false;
                }
            });

            }

        }
    }

    /**判断奇偶*/
   private int even( int n )
    {
        int num = 0;
        if(n%3==0) {
            num = 0;
        } else if(n%3==1){
            num = 1;
        } else if(n%3==2){
            num = 2;
        }
        return num;
    }

    class JigInfo{
        private String result;
        private String value;

        public String getResult() {
            return result;
        }

        public void setResult(String result) {
            this.result = result;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

}
